package com.food.user.service.impl;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.food.user.entity.ProductsType;
import com.food.user.mapper.ProductsTypeMapper;
import com.food.user.service.ProductsTypeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.food.user.util.RedisKeyUtil;
import com.food.user.util.RedisUtil;
import io.swagger.annotations.ApiModelProperty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 帅得一
 * @since 2023年06月17日
 */
@Service
public class ProductsTypeServiceImpl extends ServiceImpl<ProductsTypeMapper, ProductsType> implements ProductsTypeService {
    @Autowired
    private RedisUtil redisUtil;


    @Override
    public List<ProductsType> cacheList() {
        //从缓存中取
        if (redisUtil.hasKey(RedisKeyUtil.typeCache())){

            String value = redisUtil.getValue(RedisKeyUtil.typeCache());
            List<ProductsType> productsTypes = JSONUtil.toList(value, ProductsType.class);
            return productsTypes;
        }

        //查询状态为上架的类别
        List<ProductsType> list = list(new QueryWrapper<ProductsType>().eq("type_state", 1));

        //存进缓存
        if (!redisUtil.hasKey(RedisKeyUtil.typeCache())){
            String s = JSONUtil.toJsonStr(list);
            redisUtil.setValue(RedisKeyUtil.typeCache(),s,10L);
        }
        return list;

    }
}
